﻿Imports System.Globalization
Imports SecretaryToolData
Imports System.ComponentModel

Public Class frmPublisherCard

	'Private db As New CongregationDataContainerLocal
	Private _PublisherID As Long
	'Private workingPublisher As PublisherData
	'Private activeReport As PublisherCardData

	Private ReadOnly Property TypedPublisherCardBindingSource As VM_PublisherCard
		Get
			Return CType(PublisherCardBindingSource.DataSource, VM_PublisherCard)
		End Get
	End Property

	Private ReadOnly Property TypedAllPublisherRecordsBindingSource As BindingList(Of VM_PublisherCardRecord)
		Get
			Return CType(AllPublisherRecordsBindingSource.DataSource, BindingList(Of VM_PublisherCardRecord))
		End Get
	End Property

	Private ReadOnly Property TypedPublisherCardRecordBindingSource As VM_PublisherCardRecord
		Get
			Return CType(PublisherCardRecordBindingSource.DataSource, VM_PublisherCardRecord)
		End Get
	End Property

	Public Sub New(PublisherID As Long)

		' This call is required by the designer.
		InitializeComponent()

		_PublisherID = PublisherID

	End Sub

	Private Sub frmPublisherCard_Load(sender As Object, e As System.EventArgs) Handles Me.Load
		SetBindings()
	End Sub

	Private Sub frmPublisherCard_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
		numBooks.Focus()
	End Sub

	Private Sub SetBindings()
		PublisherCardBindingSource.DataSource = New VM_PublisherCard(_PublisherID)
		AllPublisherRecordsBindingSource.DataSource = TypedPublisherCardBindingSource.AllPublisherRecords
		PublisherCardBindingSource.ResetBindings(False)
		AllPublisherRecordsBindingSource.ResetBindings(False)

		'this must come after the above bindings are reset to make sure that it isn't overridden
		Dim workingMonth As DateTime
		If ConfigPropertyKeys.ReportLastSubmitted <> Date.MinValue Then
			workingMonth = ConfigPropertyKeys.ReportLastSubmitted
		Else
			workingMonth = Now
		End If
		PublisherCardRecordBindingSource.DataSource = TypedPublisherCardBindingSource.AllPublisherRecords.Where(Function(x) x.ReportMonth.FirstOfTheMonth = workingMonth.FirstOfTheMonth).First

		PublisherCardRecordBindingSource.ResetBindings(False)

	End Sub

	Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
		Me.Close()
	End Sub

	Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click

		If chkIsHistorical.Checked = False OrElse MessageBox.Show("Are you sure you want to saved this as historical data? By doing so, it won't be counted in this months report to the branch.", "Save as Historical?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
			TypedPublisherCardRecordBindingSource.Save()

			SetBindings()
		End If

	End Sub

	Private Sub btnCancel_Click(sender As Object, e As System.EventArgs) Handles btnCancel.Click

		SetBindings()

	End Sub

	Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click

		TypedPublisherCardRecordBindingSource.Delete()

		SetBindings()

	End Sub

	Private Sub chkIsHistorical_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkIsHistorical.CheckedChanged
		If CType(PublisherCardRecordBindingSource.DataSource, VM_PublisherCardRecord).IsHistorical AndAlso chkIsHistorical.Checked = False Then
			'this means we're trying to uncheck the box
			If MessageBox.Show("Do you mean to enter a time report that will be submitted to the branch as part of this months report? If not, and you are entering time from a publisher record card, please leave 'Is Historical' checked.", "Do you have a time slip?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.No Then
				chkIsHistorical.Checked = True
			End If
		End If
	End Sub

	'Private Sub rdoPioneer_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rdoPioneer.CheckedChanged
	'	grpPioneerHours.Visible = rdoPioneer.Checked

	'	chkBaptized.Checked = True
	'End Sub

	'Private Sub rdoSpecialPioneer_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rdoSpecialPioneer.CheckedChanged
	'	chkBaptized.Checked = True
	'End Sub

	'Private Sub rdoAuxPioneer_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rdoAuxPioneer.CheckedChanged
	'	chkBaptized.Checked = True
	'End Sub

	Private Sub FieldGotFocus(sender As System.Object, e As System.EventArgs) Handles numBooks.GotFocus, numBrochures.GotFocus, numHours.GotFocus, numCreditHours.GotFocus, numMagazines.GotFocus, numReturnVisits.GotFocus, numBibleStudies.GotFocus, txtRemarks.GotFocus
		'this only works because we've added an extension method for SelectAll on NumericUpDown
		'this is only being called when sender is a NumericUpDown or TextBox
		If TypeOf sender Is TextBox Then
			CType(sender, TextBox).SelectAll()
		ElseIf TypeOf sender Is NumericUpDown Then
			CType(sender, NumericUpDown).SelectAll()
		End If
	End Sub

	Private Sub dgvHistoricalPublisherInfo_CellEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvHistoricalPublisherInfo.CellEnter
		ColorRows()
		numBooks.Focus()
	End Sub

	Private Sub dgvHistoricalPublisherInfo_DataBindingComplete(sender As Object, e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles dgvHistoricalPublisherInfo.DataBindingComplete
		ColorRows()
	End Sub

	Private Sub dgvHistoricalPublisherInfo_RowEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvHistoricalPublisherInfo.RowEnter
		PublisherCardRecordBindingSource.DataSource = TypedPublisherCardBindingSource.AllPublisherRecords.Single(Function(x) x.ReportMonth = TypedAllPublisherRecordsBindingSource.Item(e.RowIndex).ReportMonth)
		PublisherCardRecordBindingSource.ResetBindings(False)

		grpSelectedRecord.Enabled = Not TypedPublisherCardRecordBindingSource.IsReadOnly
		btnChangeRemark.Visible = TypedPublisherCardRecordBindingSource.IsReadOnly
		btnChangePioneerCreditHours.Visible = TypedPublisherCardRecordBindingSource.IsReadOnly
		grpPioneerHours.Visible = TypedPublisherCardRecordBindingSource.IsPioneer
		lblTotalPioneerHours.Visible = TypedPublisherCardBindingSource.IsPioneer
	End Sub

	Private Sub cmboMonth_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmboMonth.SelectedIndexChanged
		For Each row As DataGridViewRow In dgvHistoricalPublisherInfo.Rows
			If CType(row.Cells(dgvcDate.Index).Value, DateTime) = cmboMonth.SelectedValue Then
				Try
					row.Cells(dgvcDate.Index).Selected = True
					Exit Sub
				Catch ex As InvalidOperationException
					'this will happen if the SelectedIndexChanged event fires in connection with databinding/rebinding
				End Try
			End If
		Next
	End Sub

	Private Sub ColorRows(Optional RowIndex As Integer? = Nothing)
		If RowIndex Is Nothing Then
			For Each row As DataGridViewRow In dgvHistoricalPublisherInfo.Rows
				ColorRows(row.Index)
			Next
		Else
			Dim row As DataGridViewRow = dgvHistoricalPublisherInfo.Rows(RowIndex)
			Dim readonlyCell As DataGridViewCell = row.Cells(dgvcIsReadOnly.Index)
			Dim hoursCell As DataGridViewCell = row.Cells(dgvcHours.Index)

			If readonlyCell.Value Then
				row.DefaultCellStyle.BackColor = Color.LightBlue
			Else
				If dgvHistoricalPublisherInfo.CurrentRow IsNot Nothing AndAlso RowIndex = dgvHistoricalPublisherInfo.CurrentRow.Index Then
					row.DefaultCellStyle.BackColor = Color.Yellow
				Else
					If hoursCell.Value = 0 Then
						row.DefaultCellStyle.BackColor = Color.LightPink
					Else
						row.DefaultCellStyle.BackColor = Color.LightGreen
					End If
				End If
			End If
		End If

	End Sub

	Private Sub btnChangeRemark_Click(sender As System.Object, e As System.EventArgs) Handles btnChangeRemark.Click
		Dim newRemark As String = InputBox("What is the new remark?", "Please enter new remark.", txtRemarks.Text)
		Dim row As DataGridViewRow = dgvHistoricalPublisherInfo.CurrentRow
		'VM_PublisherCardRecord.ChangeRemark(TypedPublisherCardRecordBindingSource.PublisherID, TypedPublisherCardRecordBindingSource.ReportMonth, TypedPublisherCardRecordBindingSource.Remarks, newRemark)
		TypedPublisherCardRecordBindingSource.ChangeRemark(TypedPublisherCardRecordBindingSource.Remarks, newRemark)
		SetBindings()
	End Sub

	Private Sub btnChangePioneerCreditHours_Click(sender As System.Object, e As System.EventArgs) Handles btnChangePioneerCreditHours.Click
		Dim newCreditHours As String = InputBox("How many credit hours should be applied?", "Please enter new number of credit hours.", numCreditHours.Text)
		If IsNumeric(newCreditHours) Then
			Dim row As DataGridViewRow = dgvHistoricalPublisherInfo.CurrentRow
			'VM_PublisherCardRecord.ChangePioneerCreditHours(TypedPublisherCardRecordBindingSource.PublisherID, TypedPublisherCardRecordBindingSource.ReportMonth, TypedPublisherCardRecordBindingSource.PioneerCreditHours, newCreditHours)
			TypedPublisherCardRecordBindingSource.ChangePioneerCreditHours(TypedPublisherCardRecordBindingSource.PioneerCreditHours, newCreditHours)
			SetBindings()
		End If
	End Sub

	Private Sub btnPrintSelectedRecords_Click(sender As System.Object, e As System.EventArgs) Handles btnPrintSelectedRecords.Click
		Dim Dates As New List(Of DateTime)
		For Each report As DataGridViewRow In dgvHistoricalPublisherInfo.SelectedRows
			Dates.Add(CType(report.DataBoundItem, VM_PublisherCardRecord).ReportMonth)
		Next

		Dim reportLocation As String = Reports.PublisherRecordCard(_PublisherID, Dates, chkPrintHeader.Checked, chkPrintTotals.Checked).FullName
		If MessageBox.Show(String.Format("Report generated and saved at{0}{1}.{0}{0}Do you want to open it?", Environment.NewLine, reportLocation), "Report Successfully Generated", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
			Process.Start(reportLocation)
		End If
	End Sub

	Private Sub chkHasCreditHours_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkHasCreditHours.CheckedChanged
		numCreditHours.Enabled = chkHasCreditHours.Checked
	End Sub
End Class